package com.ncr.travel.appmgr.client.view;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.event.logical.shared.OpenHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.ncr.travel.appmgr.client.mvp.EventBus;
import com.ncr.travel.appmgr.client.presenter.CategoryAndScreenPresenter;
import com.ncr.travel.appmgr.model.shared.Product;
import com.ncr.travel.appmgr.model.shared.ProductCategory;
import com.ncr.travel.appmgr.model.shared.Screen;
import com.ncr.travel.appmgr.style.client.AppManagerStyleClientBundle;

/**
 * @author nd185038
 * 
 */
public class CategoryAndScreenWidget extends Composite implements CategoryAndScreenView {
    private static CategoryAndScreeneUiBinder uiBinder = GWT.create(CategoryAndScreeneUiBinder.class);

    @UiTemplate("CategoryAndScreenView.ui.xml")
    interface CategoryAndScreeneUiBinder extends UiBinder<Widget, CategoryAndScreenWidget> {
    }
    
    private final AppManagerStyleClientBundle appManagerStyleClientBundle;
    @UiField
	DockLayoutPanel navPaneAndScreenDockPanel;
    
    @UiField
	FlowPanel contentWindowControlsPanel;
	//ND31012012, Commented, no more scroll panel
	//private final ScrollPanel productCatNavScrollPanel;
    @UiField
    Panel productCatNavPanel;
    @UiField
	FlowPanel contentWindowWorkPanel;
	private FlowPanel productCategoryHeaderPanel;
//	private final MenuBar contentMenuBar;
	private GraphicsEditorView graphicsEditorView;

	//ND25012012:NavigationPaneHeaderPanel
	private final FlowPanel navPaneHeaderPanel;

	private final Label navPanelHeaderLbl;
	@UiField
	Label contentHeaderLabel;
	
	@UiField
	Label contentMenuItemSelectedLabel;
	
	private Label greenScreenLabel;
	private Label subScreenHeaderLabel;
	@UiField
	Frame screenFrame;

	private Product product;
	private CategoryAndScreenPresenter presenter;
	private final EventBus eventBus;

	//ND27012012: Trying to make one productCategory disc panel only to remain open
	DisclosurePanel expandedProductCategoryDiscPanel;

	//ND27012012: Trying to make one productCategory disc panel only to remain open
	DisclosurePanel expandedSubScreenDiscPanel;

	//ND27012012:
	@UiField
	HorizontalPanel contentWindowCmdButtonsPanel;

	//ND27012012:
	private Label configBtn;

	@Inject
	public CategoryAndScreenWidget(EventBus eventBus, AppManagerStyleClientBundle appManagerStyleClientBundle) {
		System.out.println("Constructing CategoryAndScreenWidget");
		initWidget(uiBinder.createAndBindUi(this));
		this.eventBus = eventBus;
		this.appManagerStyleClientBundle = appManagerStyleClientBundle;
		navPaneHeaderPanel = new FlowPanel();
		navPaneHeaderPanel.setStyleName("navPaneHeader");

		navPanelHeaderLbl = new Label();
		navPanelHeaderLbl.setStyleName("navPanelHeaderLbl");

		//ND25012012, Add the header label to the header panel.
		navPaneHeaderPanel.add(navPanelHeaderLbl);


		this.greenScreenLabel = new Label();

		this.productCategoryHeaderPanel = new FlowPanel();
		this.productCategoryHeaderPanel.setStyleName("categoryHeaderClosed");

		this.subScreenHeaderLabel = new Label();
		this.productCategoryHeaderPanel.setStyleName("subScreenHeaderClosed");

		expandedProductCategoryDiscPanel = new DisclosurePanel();
		expandedSubScreenDiscPanel = new DisclosurePanel();
		configBtn = new Label();
	}

	@Override
	public DockLayoutPanel getNavPaneAndScreenDockPanel() {
		return navPaneAndScreenDockPanel;
	}

	@Override
	public Panel getProductCatNavPanel() {
		return productCatNavPanel;
	}

	@Override
	public FlowPanel getContentWindowWorkPanel() {
		return contentWindowWorkPanel;
	}

	@Override
	public void setProductCategoryHeaderPanel(final FlowPanel productCategoryHeaderPanel) {
		this.productCategoryHeaderPanel = productCategoryHeaderPanel;
	}

	@Override
	public FlowPanel getProductCategoryHeaderPanel() {
		return productCategoryHeaderPanel;
	}

	@Override
	public GraphicsEditorView getGraphicsEditor() {
		return graphicsEditorView;
	}

	@Override
	public Label getContentHeaderLabel() {
		return contentHeaderLabel;
	}

	@Override
	public Label getContentMenuItemSelectedLabel() {
		return contentMenuItemSelectedLabel;
	}

	/**
	 * //ND27012012: Trying to make one productCategory disc panel only to remain open
	 * @return the expandedProductCategoryDiscPanel
	 */
	public DisclosurePanel getExpandedProductCategoryDiscPanel() {
		return expandedProductCategoryDiscPanel;
	}

	/**
	 * //ND27012012: Trying to make one productCategory disc panel only to remain open
	 * @param expandedProductCategoryDiscPanel the expandedProductCategoryDiscPanel to set
	 */
	public void setExpandedProductCategoryDiscPanel(DisclosurePanel expandedProductCategoryDiscPanel) {
		this.expandedProductCategoryDiscPanel = expandedProductCategoryDiscPanel;
	}

	public void setGreenScreenLabel(final Label greenScreenLabel) {
		this.greenScreenLabel = greenScreenLabel;
	}

	@Override
	public Label getGreenScreenLabel() {
		return greenScreenLabel;
	}

	/**
	 * //ND27012012: Trying to make one SubScreen disc panel only to remain open
	 * @return the expandedSubScreenDiscPanel
	 */
	public DisclosurePanel getExpandedSubScreenDiscPanel() {
		return expandedSubScreenDiscPanel;
	}

	/**
	 * //ND27012012: Trying to make one SubScreen disc panel only to remain open
	 * @param expandedSubScreenDiscPanel the expandedSubScreenDiscPanel to set
	 */
	public void setExpandedSubScreenDiscPanel(DisclosurePanel expandedSubScreenDiscPanel) {
		this.expandedSubScreenDiscPanel = expandedSubScreenDiscPanel;
	}

	@Override
	public void setSubScreenHeaderLabel(final Label subScreenHeaderLabel) {
		this.subScreenHeaderLabel = subScreenHeaderLabel;
	}

	@Override
	public Label getSubScreenHeaderLabel() {
		return subScreenHeaderLabel;
	}

	@Override
	public Frame getScreenFrame() {
		return screenFrame;
	}

	/**
	 * //ND27012012:
	 * @return the contentWindowCmdButtonsPanel
	 */
	public HorizontalPanel getContentWindowCmdButtonsPanel() {
		return contentWindowCmdButtonsPanel;
	}

	/**
	 * //ND27012012:
	 * @return the configBtn
	 */
	public Label getConfigBtn() {
		return configBtn;
	}

	/**
	 * //ND27012012:
	 * @param configBtn the configBtn to set
	 */
	public void setConfigBtn(Label configBtn) {
		this.configBtn = configBtn;
	}

	@Override
	public void setPresenter(CategoryAndScreenPresenter presenter) {
		this.presenter = presenter;
	}

	@Override
	public void setProduct(final Product product) {
		this.product = product;
	}

	/*//ND30012012, Commented, as nav panel no more contains scroll panel.
	 * @Override
	public ScrollPanel getProductCatNavScrollPanel() {
		// TODO Auto-generated method stub
		return null;
	}*/

	@Override
	public void showView() {
		System.out.println("(CategoryAndScreenWidget) Called showView()");

		/*ND30012012, Commented as it is replaced by productCategoryNavPanel.
		 * getProductCatNavScrollPanel().clear();*/

		productCatNavPanel.clear();

		// Set the header label for the navigation pane.
		navPanelHeaderLbl.setText(product.getName());

		// Create a new Vertical Panel.
		FlowPanel productCateAndScreenVerticalNavPanel = new FlowPanel();
		productCateAndScreenVerticalNavPanel.setStyleName("productCateAndScreenVerticalNavPanel");
		productCateAndScreenVerticalNavPanel.add(navPaneHeaderPanel);

		for (final ProductCategory productCategoryItemData : product.getCategories()) {

		    final DisclosurePanel productCategoryDiscPanel = new DisclosurePanel();
			//ND27012012, Adding disclosurepanel style
			productCategoryDiscPanel.setStyleName("productCategoryDiscPanel");

			final FlowPanel productCategoryHeader = new FlowPanel();
			//ND25012012, Commented, as setting the size of size is not required now
			//productCategoryHeader.setSize("100%", "30px");
			productCategoryHeader.setStyleName("categoryHeaderClosed");

			final Label headerTextLbl = new Label(productCategoryItemData.getName());
			//ND25012012, Commented, as setting the size of text is not required now
			//headerTextLbl.setSize("100%", "30px");
			headerTextLbl.setStyleName("categoryHeaderText");

//			final AppManagerStyleClientBundle resources = GWT.create(AppManagerStyleClientBundle.class);
//			Image headerImage = new Image(resources.arrowUpPanelImage());
//			headerImage.setStyleName("categoryHeaderImage");

			productCategoryHeader.add(headerTextLbl);
			//ND25012012, Image is no more required.
			//productCategoryHeader.add(headerImage);

			setProductCategoryHeaderPanel(productCategoryHeader);

//			productCategoryHeader.setCellHorizontalAlignment(headerTextLbl, HasHorizontalAlignment.ALIGN_LEFT);
//			productCategoryHeader.setCellVerticalAlignment(headerTextLbl, HasVerticalAlignment.ALIGN_MIDDLE);
			//ND25122012, Arrow images are no more required, they are part of background images.
			/*productCategoryHeader.setCellHorizontalAlignment(headerImage, HasHorizontalAlignment.ALIGN_RIGHT);
			productCategoryHeader.setCellVerticalAlignment(headerImage, HasVerticalAlignment.ALIGN_MIDDLE);*/
			productCategoryDiscPanel.addOpenHandler(new OpenHandler<DisclosurePanel>() {
				@Override
				public void onOpen(OpenEvent<DisclosurePanel> event) {

					getContentMenuItemSelectedLabel().setText("");

					if (getContentWindowWorkPanel().getWidgetCount() > 0)
						getContentWindowWorkPanel().remove(0);

					if (getExpandedProductCategoryDiscPanel().equals(productCategoryDiscPanel) != true)
						getExpandedProductCategoryDiscPanel().setOpen(false);

					setExpandedProductCategoryDiscPanel(productCategoryDiscPanel);

					//ND27012012, Collapse the open SubScreenDiscPanel.
					getExpandedSubScreenDiscPanel().setOpen(false);

					getProductCategoryHeaderPanel().setStyleName("categoryHeaderClosed");
					setProductCategoryHeaderPanel(productCategoryHeader);
					getProductCategoryHeaderPanel().setStyleName("categoryHeaderOpen");
					/*((Image) productCategoryHeader.getWidget(1)).setResource(resources.arrowDownPanelImage());

					getGreenScreenLabel().removeStyleName("screenLabel");
					getGreenScreenLabel().setStyleName("screenLabelBlack");*/
					//ND25122012, Arrow images are no more required, they are part of background images.
					//((Image) productCategoryHeader.getWidget(1)).setResource(resources.arrowDownPanelImage());

					//ND30012012: Added conditions, to check if the style is screenLabel or subScreenLabel
					if (getGreenScreenLabel().getStyleName().equalsIgnoreCase("screenLabel")) {
						getGreenScreenLabel().removeStyleName("screenLabel");
						getGreenScreenLabel().setStyleName("screenLabelBlack");
					} else if (getGreenScreenLabel().getStyleName().equalsIgnoreCase("subScreenLabel")) {
						getGreenScreenLabel().removeStyleName("subScreenLabel");
						getGreenScreenLabel().setStyleName("subScreenLabelBlack");
					}
					getSubScreenHeaderLabel().removeStyleName("subScreenHeaderOpen");
					getSubScreenHeaderLabel().setStyleName("subScreenHeaderClosed");
					FlowPanel categoryPanel = (FlowPanel) productCategoryDiscPanel.getContent();
					if (categoryPanel.getWidgetCount() > 0) {
						if (categoryPanel.getWidget(0) instanceof Label) {
							setGreenScreenLabel((Label) categoryPanel.getWidget(0));
						}
					}
					getGreenScreenLabel().setStyleName("screenLabel");

					setContentWindowHeaderLabelAndMenuItems(getGreenScreenLabel().getText());

					getContentMenuItemSelectedLabel().setText("");

					List<Screen> screens = productCategoryItemData.getScreens();
					if (screens != null && screens.size() > 0) {
						presenter.loadScreenConfiguration(product.getKey(), productCategoryItemData.getKey() + "&" + screens.get(0).getKey());
					} else {
						presenter.loadScreenConfiguration(product.getKey(), productCategoryItemData.getKey());
					}
				}
			});

			productCategoryDiscPanel.addCloseHandler(new CloseHandler<DisclosurePanel>() {
				@Override
				public void onClose(CloseEvent<DisclosurePanel> event) {
					//ND25122012, Arrow images are no more required, they are part of background images.
					//((Image) ((HorizontalPanel) productCategoryDiscPanel.getHeader()).getWidget(1)).setResource(resources.arrowUpPanelImage());

					//ND25012012, Added more code below.
					getProductCategoryHeaderPanel().removeStyleName("categoryHeaderOpen");
					getProductCategoryHeaderPanel().setStyleName("categoryHeaderClosed");
					getProductCategoryHeaderPanel().getWidget(0).removeStyleName("categoryHeaderTextOpen");
					getProductCategoryHeaderPanel().getWidget(0).setStyleName("categoryHeaderText");
				}
			});

			productCategoryDiscPanel.setHeader(productCategoryHeader);
			//ND30012012, Size setting by CSS only.
			//productCategoryDiscPanel.setSize("100%", "100%");
			productCategoryDiscPanel.setContent(loadChildComponents(productCategoryItemData, null));
			productCateAndScreenVerticalNavPanel.add(productCategoryDiscPanel);
		}

		/*ND30012012, Commented, no more scroll panel now.
		 * getProductCatNavScrollPanel().add(productCateAndScreenVerticalNavPanel);*/
		productCatNavPanel.add(productCateAndScreenVerticalNavPanel);
	}

	/**
	 * @param itemData
	 * @return
	 */
	private FlowPanel loadChildComponents(final Object itemData, final ProductCategory productCategory) {

		FlowPanel screensFlowPanel = new FlowPanel();

		//ND30012012: Size details come from CSS.
		screensFlowPanel.setStyleName("screensFlowPanel");
		//screensVerticalPanel.setSize("100%", "100%");

		if (itemData instanceof ProductCategory) {
			final ProductCategory category = (ProductCategory) itemData;
			if (category.getScreens() == null) {
				return screensFlowPanel;
			}
			for (final Screen screenItemData : category.getScreens()) {

				// Check if the screen contains child sub screens?
				if (screenItemData.getChildScreens() == null || screenItemData.getChildScreens().size() == 0) {
					// if no, create a screen label and add it to screensPanel.
				    final FlowPanel screenLabelPanel = new FlowPanel();
				    screenLabelPanel.setStyleName("screenLanelPanel");
				    
					final Label screenLabel = new Label(screenItemData.getName());
					screenLabel.setTitle(screenItemData.getName());
					//ND27012012: Commented, Size comes from CSS file.
					//screenLabel.setSize("100%", "30px");
					screenLabel.setStyleName("screenLabelBlack");

					screenLabel.addClickHandler(new ClickHandler() {
						@Override
						public void onClick(ClickEvent event) {
							//ND27012012, Collapse the open SubScreenDiscPanel.
							getExpandedSubScreenDiscPanel().setOpen(false);

							getContentMenuItemSelectedLabel().setText("");
							// 1711: Narendra Temp Remove
							// MainAppMgrWindow.contentWindowSplitPanel.remove(MainAppMgrWindow.subMenuSelectedItemPanel);
							if (getContentWindowWorkPanel().getWidgetCount() > 0)
								getContentWindowWorkPanel().remove(0);

							setContentWindowHeaderLabelAndMenuItems(screenLabel.getText());

							getSubScreenHeaderLabel().removeStyleName("subScreenHeaderOpen");

							getSubScreenHeaderLabel().setStyleName("subScreenHeaderClosed");

							//ND30012012: Added conditions, to check if the style is screenLabel or subScreenLabel
							/*getGreenScreenLabel().removeStyleName("screenLabel");
							getGreenScreenLabel().setStyleName("screenLabelBlack");*/
							if (getGreenScreenLabel().getStyleName().equalsIgnoreCase("screenLabel")) {
								getGreenScreenLabel().removeStyleName("screenLabel");
								getGreenScreenLabel().setStyleName("screenLabelBlack");
							} else if (getGreenScreenLabel().getStyleName().equalsIgnoreCase("subScreenLabel")) {
								getGreenScreenLabel().removeStyleName("subScreenLabel");
								getGreenScreenLabel().setStyleName("subScreenLabelBlack");
							}
							setGreenScreenLabel(screenLabel);
							getGreenScreenLabel().setStyleName("screenLabel");

							getProductCategoryHeaderPanel().setStyleName("categoryHeaderClosed");
							setProductCategoryHeaderPanel((FlowPanel) ((DisclosurePanel) ((VerticalPanel) (((SimplePanel) (((FlowPanel) (((FlowPanel)(screenLabel.getParent())).getParent())).getParent())).getParent())).getParent())
									.getHeader());
							getProductCategoryHeaderPanel().setStyleName("categoryHeaderOpen");

							getContentMenuItemSelectedLabel().setText("");
							System.out.println("SETTING SCREEN KEY TO " + category.getKey() + "&" + screenItemData.getKey());
							presenter.loadScreenConfiguration(product.getKey(), category.getKey() + "&" + screenItemData.getKey());
						}
					});
					screenLabelPanel.add(screenLabel);
					screensFlowPanel.add(screenLabelPanel);

				} else // Screen contains sub screens, so create a stack panel
						// for the screen.
				{

					// Load sub screens into a panel and add the panel to the
					// vertical panel.
					final DisclosurePanel subScreenPanel = new DisclosurePanel();

					//ND27012012, Added style for subScreenPanel.
					subScreenPanel.setStyleName("subScreenPanel");

					final FlowPanel subScreenHeader = new FlowPanel();
					//ND27012012, Commented, no more sizing required, its done in CSS.
					//subScreenHeader.setSize("100%", "30px");
					subScreenHeader.setStyleName("subScreenHeader");

					final Label headerText = new Label(screenItemData.getName());
					
					headerText.setStyleName("subScreenHeaderClosed");

					final AppManagerStyleClientBundle resources = GWT.create(AppManagerStyleClientBundle.class);
					Image headerImage = new Image(resources.arrowUpPanelImage());

					subScreenHeader.add(headerText);
					//ND27012012, Commented, as header background image contains everything..
					//subScreenHeader.add(headerImage);

					headerImage.setStyleName("subScreenImage");

//					subScreenHeader.setCellHorizontalAlignment(headerText, HasHorizontalAlignment.ALIGN_LEFT);
//					subScreenHeader.setCellVerticalAlignment(headerText, HasVerticalAlignment.ALIGN_MIDDLE);

					/*//ND27012012, Commented, as header background image contains everything..
					 * subScreenHeader.setCellHorizontalAlignment(headerImage, HasHorizontalAlignment.ALIGN_RIGHT);
					subScreenHeader.setCellVerticalAlignment(headerImage, HasVerticalAlignment.ALIGN_MIDDLE);*/

					subScreenPanel.addOpenHandler(new OpenHandler<DisclosurePanel>() {
						@Override
						public void onOpen(OpenEvent<DisclosurePanel> event) {
							//ND27012012.
							if (getExpandedSubScreenDiscPanel().equals(subScreenPanel) != true)
								getExpandedSubScreenDiscPanel().setOpen(false);

							setExpandedSubScreenDiscPanel(subScreenPanel);

							getContentMenuItemSelectedLabel().setText("");
							// 1711: Narendra Temp Remove
							// MainAppMgrWindow.contentWindowSplitPanel.remove(MainAppMgrWindow.subMenuSelectedItemPanel);
							if (getContentWindowWorkPanel().getWidgetCount() > 0)
								getContentWindowWorkPanel().remove(0);
							getSubScreenHeaderLabel().removeStyleName("subScreenHeaderOpen");
							getSubScreenHeaderLabel().setStyleName("subScreenHeaderClosed");
							setSubScreenHeaderLabel(headerText);
							getSubScreenHeaderLabel().setStyleName("subScreenHeaderOpen");
							//ND27012012, Commented, as header background image contains everything..
							//((Image) subScreenHeader.getWidget(1)).setResource(resources.arrowDownPanelImage());

							//ND30012012: Added conditions, to check if the style is screenLabel or subScreenLabel
							if (getGreenScreenLabel().getStyleName().equalsIgnoreCase("screenLabel")) {
								getGreenScreenLabel().removeStyleName("screenLabel");
								getGreenScreenLabel().setStyleName("screenLabelBlack");
							} else if (getGreenScreenLabel().getStyleName().equalsIgnoreCase("subScreenLabel")) {
								getGreenScreenLabel().removeStyleName("subScreenLabel");
								getGreenScreenLabel().setStyleName("subScreenLabelBlack");
							}
							/*((Image) subScreenHeader.getWidget(1)).setResource(resources.arrowDownPanelImage());

							getGreenScreenLabel().removeStyleName("screenLabel");
							getGreenScreenLabel().setStyleName("screenLabelBlack");*/
							FlowPanel subScreenPanelContent = (FlowPanel) subScreenPanel.getContent();
							if (subScreenPanelContent.getWidgetCount() > 0) {
								setGreenScreenLabel((Label) ((FlowPanel)subScreenPanelContent.getWidget(0)).getWidget(0));
							}

							//ND31012012, Changed from screenLabel to subScreenLabel.
							getGreenScreenLabel().setStyleName("subScreenLabel");

							setContentWindowHeaderLabelAndMenuItems(getGreenScreenLabel().getText());

							getContentMenuItemSelectedLabel().setText("");
							// product.getName(),
							// productCategoryItemData.getName() + "&" +
							// productCategoryItemData.getScreens().get(0).getName()
							String childKey = screenItemData.getChildScreens().get(0).getKey();
							System.out.println("SETTING SCREEN KEY TO " + childKey);
							presenter.loadScreenConfiguration(product.getKey(), category.getKey() + "&" + screenItemData.getKey() + "&" + childKey);
						}
					});

					subScreenPanel.addCloseHandler(new CloseHandler<DisclosurePanel>() {
						@Override
						public void onClose(CloseEvent<DisclosurePanel> event) {
							//ND27012012, Commented, as header background image contains everything..
							//((Image) ((HorizontalPanel) subScreenPanel.getHeader()).getWidget(1)).setResource(resources.arrowUpPanelImage());

						}
					});

					subScreenPanel.setHeader(subScreenHeader);

					//ND30012012: Commented, Size comes from CSS.
					//subScreenPanel.setSize("100%", "100%");

					subScreenPanel.setContent(loadChildComponents(screenItemData, category));// ,
					// screenItemData.getScreenName(),
					// false);

					screensFlowPanel.add(subScreenPanel);
				}
			}
		} else if (itemData instanceof Screen) {

			for (final Screen screenItemData : ((Screen) itemData).getChildScreens()) {

                final FlowPanel screenLabelPanel = new FlowPanel();
                screenLabelPanel.setStyleName("screenLanelPanel");
                
				final Label screenLabel = new Label(screenItemData.getName());
				
				screenLabel.setTitle(screenItemData.getName());

				//ND31012012, Changed from screenLabelBlack to suScreenLabelBlack.
				screenLabel.setStyleName("subScreenLabelBlack");

				//ND27012012, Commented, no more sizing required, its done in CSS.
				//screenLabel.setSize("100%", "30px");

				screenLabel.addClickHandler(new ClickHandler() {
					@Override
					public void onClick(ClickEvent event) {

						getContentMenuItemSelectedLabel().setText("");
						if (getContentWindowWorkPanel().getWidgetCount() > 0)
							getContentWindowWorkPanel().remove(0);

						getSubScreenHeaderLabel().setStyleName("subScreenHeaderClosed");
						setSubScreenHeaderLabel((Label) ((FlowPanel) ((DisclosurePanel) ((VerticalPanel) ((SimplePanel) ((FlowPanel) ((FlowPanel)(screenLabel.getParent())).getParent()).getParent()).getParent()).getParent()).getHeader())
								.getWidget(0));

						getSubScreenHeaderLabel().setStyleName("subScreenHeaderOpen");

						setContentWindowHeaderLabelAndMenuItems(screenLabel.getText());
						/*getGreenScreenLabel().removeStyleName("screenLabel");
						getGreenScreenLabel().setStyleName("screenLabelBlack");*/
						//ND30012012: Added conditions, to check if the style is screenLabel or subScreenLabel
						if (getGreenScreenLabel().getStyleName().equalsIgnoreCase("screenLabel")) {
							getGreenScreenLabel().removeStyleName("screenLabel");
							getGreenScreenLabel().setStyleName("screenLabelBlack");
						} else if (getGreenScreenLabel().getStyleName().equalsIgnoreCase("subScreenLabel")) {
							getGreenScreenLabel().removeStyleName("subScreenLabel");
							getGreenScreenLabel().setStyleName("subScreenLabelBlack");
						}
						setGreenScreenLabel(screenLabel);
						//ND30012012: Commented for screenLabel and re-added style as subScreenLabel
						//view.getGreenScreenLabel().setStyleName("screenLabel");
						getGreenScreenLabel().setStyleName("subScreenLabel");

						// Temporary code : for blank screen ton the buttom
						// vertical split panel.
						getContentMenuItemSelectedLabel().setText("");

						System.out.println("SETTING SCREEN KEY TO " + screenItemData.getKey());
						presenter.loadScreenConfiguration(product.getKey(), productCategory.getKey() + "&" + ((Screen) itemData).getKey() + "&" + screenItemData.getKey());
					}
				});
				screenLabelPanel.add(screenLabel);
				screensFlowPanel.add(screenLabelPanel);
			}
		}

		return screensFlowPanel;
	}

	/**
	 * @param screenLabelText
	 */
	private void setContentWindowHeaderLabelAndMenuItems(final String screenLabelText) {

		getContentHeaderLabel().setText(screenLabelText);
		// selectedScreen = screenLabelText;
		// Clear all the present menu items.
		//view.getContentMenuBar().clearItems();

		contentWindowCmdButtonsPanel.clear();

		final Label lblText = new Label("Text");
		lblText.setStyleName("lblTextNRule");
		lblText.setWidth("68px");

		contentWindowCmdButtonsPanel.add(lblText);

		lblText.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				if (getContentWindowWorkPanel().getWidgetCount() > 0)
					getContentWindowWorkPanel().remove(0);

				getContentMenuItemSelectedLabel().setText("Text");
				
				if (getConfigBtn().getStyleName().equalsIgnoreCase("lblTextNRuleGreen")) {
					//Previous green is either Graphics or Functions button.
					//Reset the previous button's style to default.
					getConfigBtn().removeStyleName("lblTextNRuleGreen");
					getConfigBtn().setStyleName("lblTextNRule");
				} else if (getConfigBtn().getStyleName().equalsIgnoreCase("lblTextNRuleGreen")) {
					//Previous green is Text or Rule button.
					//Reset the previous button's style to default.
					getConfigBtn().removeStyleName("lblTextNRuleGreen");
					getConfigBtn().setStyleName("lblTextNRule");
				} else {
					//Previous green is Function button.
					//Reset the previous button's style to default.
					getConfigBtn().removeStyleName("lblTextNRuleGreen");
					getConfigBtn().setStyleName("lblTextNRule");
				}

				setConfigBtn(lblText);

				getConfigBtn().setStyleName("lblTextNRuleGreen");

				presenter.displayTextUi();
			}

		});

		final Label lblGraphics = new Label("Graphics");
		lblGraphics.setStyleName("lblTextNRule");
		lblGraphics.setWidth("94px");
		contentWindowCmdButtonsPanel.add(lblGraphics);

		lblGraphics.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				if (getContentWindowWorkPanel().getWidgetCount() > 0)
					getContentWindowWorkPanel().remove(0);

				getContentMenuItemSelectedLabel().setText("Graphics");

				if (getConfigBtn().getStyleName().equalsIgnoreCase("lblTextNRuleGreen")) {
					//Previous green is either Graphics or Functions button.
					//Reset the previous button's style to default.
					getConfigBtn().removeStyleName("lblTextNRuleGreen");
					getConfigBtn().setStyleName("lblTextNRule");
				} else if (getConfigBtn().getStyleName().equalsIgnoreCase("lblTextNRuleGreen")) {
					//Previous green is Text or Rule button.
					//Reset the previous button's style to default.
					getConfigBtn().removeStyleName("lblTextNRuleGreen");
					getConfigBtn().setStyleName("lblTextNRule");
				} else {
					//Previous green is Function button.
					//Reset the previous button's style to default.
					getConfigBtn().removeStyleName("lblTextNRuleGreen");
					getConfigBtn().setStyleName("lblTextNRule");
				}

				setConfigBtn(lblGraphics);

				getConfigBtn().setStyleName("lblTextNRuleGreen");

				if (getGreenScreenLabel().getText().equalsIgnoreCase("Logo") || getGreenScreenLabel().getText().equalsIgnoreCase("Find By Options")) {

					getContentMenuItemSelectedLabel().setText("Graphics");

					presenter.displayGraphicsUi();
				} else {
					getContentWindowWorkPanel().add(new FlowPanel());
				}
			}

		});

		final Label lblFunctions = new Label("Functions");
		lblFunctions.setStyleName("lblTextNRule");
		lblFunctions.setWidth("94px");

		contentWindowCmdButtonsPanel.add(lblFunctions);

		lblFunctions.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				if (getContentWindowWorkPanel().getWidgetCount() > 0)
					getContentWindowWorkPanel().remove(0);

				getContentMenuItemSelectedLabel().setText("Functions");

				if (getConfigBtn().getStyleName().equalsIgnoreCase("lblTextNRuleGreen")) {
					//Previous green is either Graphics or Functions button.
					//Reset the previous button's style to default.
					getConfigBtn().removeStyleName("lblTextNRuleGreen");
					getConfigBtn().setStyleName("lblTextNRule");
				} else if (getConfigBtn().getStyleName().equalsIgnoreCase("lblTextNRuleGreen")) {
					//Previous green is Text or Rule button.
					//Reset the previous button's style to default.
					getConfigBtn().removeStyleName("lblTextNRuleGreen");
					getConfigBtn().setStyleName("lblTextNRule");
				} else {
					//Previous green is Function button.
					//Reset the previous button's style to default.
					getConfigBtn().removeStyleName("lblTextNRuleGreen");
					getConfigBtn().setStyleName("lblTextNRule");
				}

				setConfigBtn(lblFunctions);

				getConfigBtn().setStyleName("lblTextNRuleGreen");

				//				if (getGreenScreenLabel().getText().equalsIgnoreCase("Add Mileage Plan") || getGreenScreenLabel().getText().equalsIgnoreCase("Secure Flight Data Entry")
				//						|| getGreenScreenLabel().getText().equalsIgnoreCase("Offer") || getGreenScreenLabel().getText().equalsIgnoreCase("Thank You-1")
				//						|| getGreenScreenLabel().getText().equalsIgnoreCase("Language Options"))

				presenter.displayFunctionsUi();
			}

		});

		final Label lblRules = new Label("Rules");
		lblRules.setStyleName("lblTextNRule");
		lblRules.setWidth("68px");
		contentWindowCmdButtonsPanel.add(lblRules);

		lblRules.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				if (getContentWindowWorkPanel().getWidgetCount() > 0)
					getContentWindowWorkPanel().remove(0);

				getContentMenuItemSelectedLabel().setText("Rules");

				if (getConfigBtn().getStyleName().equalsIgnoreCase("lblTextNRuleGreen")) {
					//Previous green is either Graphics or Functions button.
					//Reset the previous button's style to default.
					getConfigBtn().removeStyleName("lblTextNRuleGreen");
					getConfigBtn().setStyleName("lblTextNRule");
				} else if (getConfigBtn().getStyleName().equalsIgnoreCase("lblTextNRuleGreen")) {
					//Previous green is Text or Rule button.
					//Reset the previous button's style to default.
					getConfigBtn().removeStyleName("lblTextNRuleGreen");
					getConfigBtn().setStyleName("lblTextNRule");
				} else {
					//Previous green is Function button.
					//Reset the previous button's style to default.
					getConfigBtn().removeStyleName("lblTextNRuleGreen");
					getConfigBtn().setStyleName("lblTextNRule");
				}

				setConfigBtn(lblRules);

				getConfigBtn().setStyleName("lblTextNRuleGreen");

				/*if (getGreenScreenLabel().getText().equalsIgnoreCase("Add Mileage Plan") || getGreenScreenLabel().getText().equalsIgnoreCase("Special Service Requests Code")
						|| getGreenScreenLabel().getText().equalsIgnoreCase("Offer"))*/
				// MM setRuleTable(screenLabelText, screenConfiguration);

				presenter.displayRulesUi();
			}

		});

		/*getContentHeaderLabel().setText(screenLabelText);
		// selectedScreen = screenLabelText;
		// Clear all the present menu items.
		getContentMenuBar().clearItems();

		Command graphicsCommand = new Command() {
			@Override
			public void execute() {

				if (getContentWindowWorkPanel().getWidgetCount() > 0)
					getContentWindowWorkPanel().remove(0);

				getContentMenuItemSelectedLabel().setText("Graphics");
				if (getGreenScreenLabel().getText().equalsIgnoreCase("Logo") || getGreenScreenLabel().getText().equalsIgnoreCase("Find By Options")) {

					getContentMenuItemSelectedLabel().setText("Graphics");

					presenter.displayGraphicsUi();
				} else {
					getContentWindowWorkPanel().add(new HorizontalPanel());
				}
			}
		};

		// This is for rule command, when user select the Rule command, the call
		// will come to here for the corresponding screen.
		Command ruleCommand = new Command() {
			@Override
			public void execute() {

				if (getContentWindowWorkPanel().getWidgetCount() > 0)
					getContentWindowWorkPanel().remove(0);

				getContentMenuItemSelectedLabel().setText("Rules");

				if (getGreenScreenLabel().getText().equalsIgnoreCase("Add Mileage Plan") || getGreenScreenLabel().getText().equalsIgnoreCase("Special Service Requests Code")
						|| getGreenScreenLabel().getText().equalsIgnoreCase("Offer"))
					// MM setRuleTable(screenLabelText, screenConfiguration);
					presenter.displayRulesUi(screenLabelText);
			}
		};

		Command functionCommand = new Command() {

			@Override
			public void execute() {
				// Window.alert("Function Command fired..");
				if (getContentWindowWorkPanel().getWidgetCount() > 0)
					getContentWindowWorkPanel().remove(0);
				getContentMenuItemSelectedLabel().setText("Functions");
				if (getGreenScreenLabel().getText().equalsIgnoreCase("Add Mileage Plan") || getGreenScreenLabel().getText().equalsIgnoreCase("Secure Flight Data Entry")
						|| getGreenScreenLabel().getText().equalsIgnoreCase("Offer") || getGreenScreenLabel().getText().equalsIgnoreCase("Thank You-1")
						|| getGreenScreenLabel().getText().equalsIgnoreCase("Language Options"))

					presenter.displayFunctionsUi(screenLabelText);
			}
		};

		Command textCommand = null;
		final MenuItem item1 = new MenuItem("Text", textCommand);
		item1.setStyleName("contentMenuItem");

		textCommand = new Command() {

			@Override
			public void execute() {
				if (getContentWindowWorkPanel().getWidgetCount() > 0)
					getContentWindowWorkPanel().remove(0);
				item1.setStyleName("configMenuItemSelected");
				getContentMenuItemSelectedLabel().setText("Text");
				presenter.displayTextUi();
			}
		};
		item1.setCommand(textCommand);
		item1.setStyleName("contentMenuItem");

		MenuItem item2 = new MenuItem("Graphics", false, graphicsCommand);

		item2.setStyleName("contentMenuItem");

		MenuItem item3 = new MenuItem("Functions ", false, functionCommand);

		item3.setStyleName("contentMenuItem");

		MenuItem item4 = new MenuItem("Rules", false, ruleCommand);

		item4.setStyleName("contentMenuItem");

		MenuItemSeparator menuSeparator = new MenuItemSeparator();
		menuSeparator.addStyleName("menuItemSeparator");

		getContentMenuBar().addItem(item1);
		getContentMenuBar().addSeparator(menuSeparator);
		getContentMenuBar().addItem(item2);
		MenuItemSeparator menuSeparator1 = new MenuItemSeparator();
		menuSeparator1.addStyleName("menuItemSeparator");
		getContentMenuBar().addSeparator(menuSeparator1);
		getContentMenuBar().addItem(item3);
		MenuItemSeparator menuSeparator2 = new MenuItemSeparator();
		menuSeparator2.addStyleName("menuItemSeparator");
		getContentMenuBar().addSeparator(menuSeparator2);
		getContentMenuBar().addItem(item4);*/
	}

	/**
	 * This is a UiFactory that returns the AppManagerStyleClientBundle to the
	 * UiBinder template when it tries to access the resource; this factory lets
	 * us control how and when the AppManagerStyleClientBundle is created and how
	 * many instances we have.
	 * 
	 * @return
	 */
	@UiFactory
	public AppManagerStyleClientBundle getAppManagerStyleClientBundle() {
		return appManagerStyleClientBundle;
	}
}
